$(document).ready(function(){
    queryUrlsData(1);
    //添加关键词
    $("#addKeyWord").click(function(){
        window.top.location.href =WEB_ROOT+"/key/addKeyWord";
    });
    //添加关联分组
    $("#addKey2Group").click(function(){
        var keyWordId = $("input[name='keySelect']:checked").val();
        if(""==keyWordId || null == keyWordId){
            eDialog.alert('请选择关键词记录!');
            return;
        }
        //查看状态是否为无效，无效记录则不能进行绑定
        var state = $("input[name='keySelect']:checked").attr("state");
        if("1"!=state){
            eDialog.alert('无效记录不能关联组!');
            return;
        }
        window.top.location.href =WEB_ROOT+"/key/addKey2Group?id="+keyWordId;
    });
    //删除关键词
    $("#delKeyWord").click(function(){
        var keyWordId = $("input[name='keySelect']:checked").val();
        if(keyWordId){
            eDialog.confirm("您确认要删除关键词吗？", {
                buttons : [{
                    caption : '确认',
                    callback : function(){
                        eDialog.alert('success','保存成功！');
                        $.ajax({
                            url: GLOBAL.WEBROOT+"/key/delKeyWord",
                            data:{
                                keyid:keyWordId
                            },
                            type:"post",
                            dataType:"json",
                            async:true,
                            cache:true,
                            success: function (data){
                                //从后台中获取到数据添加到树结构中
                                if(data.flag == "success"){
                                    queryUrlsData(1);
                                }
                            },
                            error:function(XMLHttpRequest, textStatus, errorThrown){

                            }
                        });
                    }
                },{
                    caption : '取消',
                    callback : $.noop
                }]
            });
        }else{
            eDialog.alert('请选择记录');
            return;
        }

    });
});

function queryUrlsData(pageNo){
    var params = {
        keyname:$.trim($("#keyname").val()),
        state:$("#id-state").val(),
        pageNo:pageNo,
        pageSize:5
    };

    ajaxLoadPage(params,WEB_ROOT+"/key/keylist",$("#keylist-div-id"),function(){
        $("#urlPagerId").pager({callback:function(pageNo){
            queryUrlsData(pageNo);
        }});
    });
}